07. While Loop 3
While Loop 3
Start Quiz:
# Great! We now want to create a new list that contains the counts
# of all occurrances of every number seen in the randomly generated
# list. That means we want counts of all occurrences of all numbers
# from 0 through 10 in our randomly generated list.
# Let's store our counts in a list of length 11
# with zeros filled in.
# We can multiply a list construct to create a list with the same
# elements n number of times.
count_list = [0] * 11
# Check that we have a list of length 11 with all 0 elements
print count_list
# We use this list to store our count of numbers 0 to 10 - take note
# that total numbers 0 to 10 is 11. We can use the index number of
# each element to refer to the count of our target
# number. Our target number is actually the index of the list.
# For example, assume count_list looks like this:
count_list = [1,2,3,2,2,1,1,2,3,1,2]
# Let's print out the occurrences for the numbers 0, 4, 5, and 6
print count_list[0]
print count_list[4]
print count_list[5]
print count_list[6]
# Therefore, for our output, we want a count_list that looks like:
# [1,2,3,2,2,1,1,2,3,1,2]
# Here's our code that we coded before
import random
# Create random list of integers using while loop --------------------
random_list = []
list_length = 20
while len(random_list) < list_length:
random_list.append(random.randint(0,10))
# --------------------------------------------------------------------
# Initialize count_list for every integer between 0 and 10.
# A number will correspond to an index of this count_list
# Therefore if we see that there are 3 occurrences of the number 4,
# we assign count_list[4] = 3, if there are 5 occurrences of the
# number 6, we assign count_list[6] = 5
count_list = [0] * 11
index = 0
# Write code here to loop through every number in random_list and
# update count_list appropriately
# Check the list we created
print count_list
# If we coded everything correctly, the sum of all of the numbers
# in count_list should be 20
print sum(count_list)
Solution:
Breaking Down the Problem
Let's first identify what are the inputs and what are the outputs (or results) that we want to obtain.
The inputs are:
- A list of 20 randomly generated integers
The output is:
A list of integers where each number (let's call it "n") represents the count of an integer that occurs n number of times in the list of randomly generated integers.
For example, if the number 4 is in the randomly generated list 5 times, then we will have another list that contains the number 5 at index 4 of this list. If the number 6 is in the randomly generated list 2 times, then our output list will have the number 2 at index 6 of this list. The output list so far would look like this:
[0,0,0,0,5,0,2,0,0,0,0]
What To Do
Let's see if we can write an outline of what to do if we were to do this manually on pen and paper:
- Loop through each element in the randomly generated list
- Get the current number in our list
- We now need to increment our count for this number by one
- Let's get the current count of this number by checking our output list and then increment that count by one
- Are we at the end of the loop?
- If not, we loop back up and go through steps 1 to 5 again while we are still going through the list
Translation
Let's step through these steps and translate these steps into computer code.
1. Loop through each element in the list
It seems like we first need to set up the loop structure to loop through each element in the list. In order to do this, we should setup some variables to hold the current index of the list and create the initial count_list:
count_list = [0]*11
index = 0
We can now set up our loop structure:
while index < len(random_list):
# Put other code here
index = index + 1
Please keep in mind how we are already adding index = index + 1 to the loop. This code is crucial to guarantee that the computer not step into an infinite loop. For most loops, we want to always clearly define a stopping point. In this case, the stopping point is when the number index is greater than the length of our list.
2. Get current number in our list
We get the number by accessing the list with the current index:
number = random_list[index]
3-4: We now need to increment our count for this number by one.
Since the index of count_list will serve as our lookup, we can increment the count of our current number like this:
count_list[number] = count_list[number] + 1
5: Are we at the end of loop? If not, we loop back up and go through the while loop instructions all over again.
Our loop construct already takes care of this criteria because at the top of our loop, we are always checking whether our index number is still less than the length of the random list: index < len(random_list)
We use the logic: if the index number is less than the length of our list, then we can safely say that whenever we access the list with index, we will never create an error and will be able access elements in the list with the number index.
Answer Code
count_list = [0]*11
count = 0
while index < len(random_list):
number = random_list[index]
count_list[number] = count_list[number] + 1
index = index + 1
Full Answer Code
import random
random_list = []
list_length = 20
while len(random_list) < list_length:
random_list.append(random.randint(0,10))
count_list = [0] * 11
index = 0
count = 0
while index < len(random_list):
number = random_list[index]
count_list[number] = count_list[number] + 1
index = index + 1